﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EzFrameWork;
using System.Threading;
using System;
using System.Text;
public class TCPClient : MonoBehaviour
{
    EzTCPClient tCPClient = null;
    List<Thread> threadsPool = new List<Thread>();
    public string IP = "127.0.0.1";
    public int Port = 0;
    int d;
    private void OnGUI()
    {
        if (tCPClient != null)
        {
            tCPClient.ShowOnGUI();
            GUI.Label(new Rect(0, 200, 200, 80), d.ToString());
        }
    }

    private void Start()
    {
        StartLink(IP, Port);
    }


    //连接服务器
    public void StartLink(string URL, int Port)
    {
        if (tCPClient != null && tCPClient.IsOnline)
        {
            tCPClient.CloseLink();//释放之前的连接
            tCPClient = null;
            Debug.Log("释放之前的链接");
        }
        tCPClient = new EzTCPClient(URL, Port);
        tCPClient.OpenLink();
        tCPClient.Response += Response;
        Debug.Log("成功打开连接");
    }



    /// <summary>
    /// 关闭与服务器的链接
    /// </summary>
    public void CloseLink()
    {
        if (tCPClient != null)
        {
            tCPClient.CloseLink();
        }
        CloseAllThread();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            StartLink(IP, Port);
            tCPClient.Response += Response;
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            CloseLink();
        }
    }

    private void Response(MessageType arg1, byte[] arg2)
    {
        string str;
        switch (arg1)
        {
            case MessageType.HERTPACK:
                Hert hert = EzDataRWS.BytesToObject<Hert>(arg2);
                //string fps =(hert.Time - DateTime.Now).ToString());
                TimeSpan timeSpan = hert.Time - DateTime.Now;
                int delay = timeSpan.Milliseconds;
                d = delay;
                Debug.Log("客户端：" + tCPClient.IDText + "接受到服务端：" + hert.ID + "的心跳包" + "延迟为：" + delay);
                break;
            case MessageType.FILE:
                
                break;
            case MessageType.JSON:
                DataPack dataPack = EzDataRWS.BytesToObject<DataPack>(arg2);
                break;
            case MessageType.SENDID:
                tCPClient.IDText = Encoding.UTF8.GetString(arg2);
                Debug.Log("这个客户端的ID为：" + tCPClient.IDText);
                break;
            case MessageType.TEXT:
                str = Encoding.UTF8.GetString(arg2);
                Debug.Log(str);
                break;
            case MessageType.BYE:
                Debug.Log("服务端主动断开链接");
                break;
            default:
                break;
        }
    }

    public void SendData()
    {
        
    }

    private void CloseAllThread()
    {
        foreach (var item in threadsPool)
        {
            item.Abort();
        }
        Debug.Log("关闭所有临时线程");
    }

    private void OnDestroy()
    {
        CloseLink();
    }
}
